package cn.itcast.myapplication.viewemodel;

import android.app.Application;
import android.app.TaskInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import java.util.List;

import cn.itcast.myapplication.room.Word;
import cn.itcast.myapplication.room.WordDao;
import cn.itcast.myapplication.room.WordDatabase;
import cn.itcast.myapplication.room.WordRepository;

public class WordViewModel extends AndroidViewModel {

    private WordRepository wordRepository;

    public MutableLiveData<Integer> number;



    public WordViewModel(@NonNull Application application) {

        super(application);

        wordRepository=new WordRepository(application);

      }

    //获取搜索后的结果
    public LiveData<List<Word>> findWordsWithPatten(String patten){

        return wordRepository.findWordsWithPatten("%"+patten+"%");

    }

    //获取所有列表中的数据
    public LiveData<List<Word>> getAllWordsLive() {

        return wordRepository.getAllWordsLive();

      }

    //添加
    public void insertWord(Word...words){

         wordRepository.insertWord(words);

    }

    //修改
    public void updateWord(Word...words){

        wordRepository.updateWord(words);

    }

    //删除
    public void deleteWord(Word...words){

        wordRepository.deleteWord(words);

    }

    //删除全部
    public void DeleteAllWord(){

        wordRepository.DeleteAllWord();

    }

    public MutableLiveData<Integer> getNumber() {

        if (number==null) {

            number = new MutableLiveData<>();

            number.setValue(0);

        }
        return number;

    }

    public void add(int numbers){

        number.setValue(number.getValue()+numbers);

    }

 }